package com.aw.baselib.util

import java.io.Serializable
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * Created by hzw
 */
object AwDataUtil {
    /**
     * 判断String是否为空 长度是否为0
     * @param str
     * @return 当字符串长度为空或长度为0时，返回true
     */
    @JvmStatic
    fun isEmpty(str: String?): Boolean {
        return if ("" == str || str == null || str.trim { it <= ' ' } == "null") {
            true
        } else false
    }

    /**
     * 判断List是否为空 或者长度为0
     * @param list
     * @return 当List为空或长度为0时，返回true
     */
    fun isEmpty(list: List<*>?): Boolean {
        return null == list || list.size == 0
    }

    /**
     * 判断数组是否为空 或者长度为0
     * @param obj
     * @return
     */
    fun isEmpty(obj: Array<Any?>?): Boolean {
        return if (obj == null || obj.size == 0) {
            true
        } else false
    }

    /**
     * 判断Set是否为空 或者长度0
     * @param set
     * @return 当Set为空或长度为0时，返回true
     */
    fun isEmpty(set: Set<*>?): Boolean {
        return null == set || set.size == 0
    }

    /**
     * 判断Map是是否为空 或者长度为0
     * @param map
     * @return 当Map为空或长度为0时，返回true
     */
    fun isEmpty(map: Map<*, *>?): Boolean {
        return null == map || map.size == 0
    }

    fun isEmpty(serializable: Serializable?): Boolean {
        return if (null == serializable) {
            true
        } else false
    }

    @JvmStatic
    fun getIntValue(param: String): Int {
        return if (isEmpty(param)) {
            0
        } else try {
            param.toInt()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
            0
        }
    }

    fun getIntValueDefault0(param: String): Int {
        return if (isEmpty(param)) {
            0
        } else try {
            param.toInt()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
            0
        }
    }

    fun getDouobleValue(param: String): Double {
        return if (isEmpty(param)) {
            (-1).toDouble()
        } else try {
            param.toDouble()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
            (-1).toDouble()
        }
    }

    fun getDoubleDecimalValue(param: String?): Double {
        return if (isEmpty(param)) {
            (-1).toDouble()
        } else try {
            BigDecimal(param).toDouble()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
            (-1).toDouble()
        }
    }

    fun getDoubleDecimalValue(param: String?, dotNum: Int): Double {
        return if (isEmpty(param)) {
            (-1).toDouble()
        } else try {
            BigDecimal(param).setScale(dotNum, RoundingMode.HALF_UP).toDouble()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
            (-1).toDouble()
        }
    }

    @JvmStatic
    fun getLongValue(param: String): Long {
        return if (isEmpty(param)) {
            -1
        } else try {
            param.toLong()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
            -1
        }
    }

    fun removeItem(list: MutableList<Any?>) {
        val iterator = list.iterator()
        while (iterator.hasNext()) {
            val temp = iterator.next()
            if (true) {
                iterator.remove()
            }
        }
    }

    fun formatStr(params: String): String {
        return if (isEmpty(params)) {
            "0"
        } else {
            subZeroAndDot(params)
        }
    }

    fun subZeroAndDot(s: String): String {
        var s = s
        if (s.indexOf(".") > 0) {
            s = s.replace("0+?$".toRegex(), "") //去掉多余的0
            s = s.replace("[.]$".toRegex(), "") //如最后一位是.则去掉
        }
        return s
    }

    fun addZero(param: Int): String {
        var paramStr = param.toString()
        if (param < 10) {
            paramStr = "0$paramStr"
        }
        return paramStr
    }
}